﻿using System;
using Autofac;

namespace ShuttleBus.Config
{
    public class Configuration
    {
        internal Configuration()
        {
            var builder = new ContainerBuilder();
            builder.RegisterModule(new ShuttleBusModule());
            UnitOfWork.Instance.ApplicationContainer = builder.Build();
        }

        public Configuration IntoContainer(IContainer container)
        {
            UnitOfWork.Instance.ApplicationContainer.Dispose();
            UnitOfWork.Instance.ApplicationContainer = container;

            var builder = new ContainerBuilder();
            builder.RegisterModule(new ShuttleBusModule());
            builder.Update(container);
            return this;
        }

        public Configuration WithRegistrations(Action<ContainerBuilder> registrations)
        {
            var builder = new ContainerBuilder();
            registrations.Invoke(builder);
            builder.Update(UnitOfWork.Instance.ApplicationContainer);
            return this;
        }
    }
}